import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _class;

/**
 * EditDataTable
 * @date 2019-08-14
 */
import React from 'react';
import { Form } from 'antd';
import { Bind } from 'lodash-decorators';
import { Button as ButtonPermission } from 'components/Permission';
import EditTable from 'components/EditTable';
import Lov from 'components/Lov';
import intl from 'utils/intl';
import { tableScrollWidth } from 'utils/utils';
import { VERSION_IS_OP } from 'utils/config';
var rowKey = '_mockId';
var EditDataTable = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(EditDataTable, _React$Component);

  var _super = _createSuper(EditDataTable);

  function EditDataTable() {
    var _this;

    _classCallCheck(this, EditDataTable);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {};
    return _this;
  }

  _createClass(EditDataTable, [{
    key: "handleAddBtnClick",
    value: function handleAddBtnClick() {
      var onTableAdd = this.props.onTableAdd;
      onTableAdd();
    }
  }, {
    key: "handleDeleteBtnClick",
    value: function () {
      var _handleDeleteBtnClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var onTableDelete, _this$state$selectedR, selectedRows;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                onTableDelete = this.props.onTableDelete;
                _this$state$selectedR = this.state.selectedRows, selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
                _context.next = 4;
                return onTableDelete(selectedRows);

              case 4:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function handleDeleteBtnClick() {
        return _handleDeleteBtnClick.apply(this, arguments);
      }

      return handleDeleteBtnClick;
    }() // Table

  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$role = _this$props.role,
          role = _this$props$role === void 0 ? {} : _this$props$role,
          tenantRoleLevel = _this$props.tenantRoleLevel;
      var roleId = role.id;
      return [{
        dataIndex: 'realName',
        title: intl.get('ciam.roleManagement.model.roleManagement.userLoginName').d('用户名'),
        render: function render(realName, record) {
          var _status = record._status;

          if (_status === 'create') {
            var form = record.$form;
            return /*#__PURE__*/React.createElement(Form.Item, null, form.getFieldDecorator('id', {
              initialValue: record.id,
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('ciam.roleManagement.model.roleManagement.userLoginName').d('用户名')
                })
              }]
            })( /*#__PURE__*/React.createElement(Lov, {
              allowClear: false,
              queryParams: {
                roleId: roleId
              },
              style: {
                width: '100%'
              },
              code: tenantRoleLevel ? 'CIAM.USER.ORG' : 'CIAM.USER',
              textValue: realName,
              onChange: function onChange(userId, userRecord) {
                _this2.handleUserChange(userId, userRecord, record);
              }
            })));
          } else {
            return realName;
          }
        }
      }, !VERSION_IS_OP && {
        title: intl.get('ciam.roleManagement.model.roleManagement.tenant').d('所属租户'),
        dataIndex: 'tenantName',
        render: function render(tenantName, record) {
          var _status = record._status;

          if (_status === 'create' || _status === 'update') {
            var form = record.$form;
            return form.getFieldDecorator('tenantName', {
              initialValue: tenantName
            })( /*#__PURE__*/React.createElement(React.Fragment, null, form.getFieldDecorator('tenantName')));
          } else {
            return tenantName;
          }
        }
      }].filter(Boolean);
    }
  }, {
    key: "handleUserChange",
    value: function handleUserChange(userId, userRecord, record) {
      var form = record.$form;
      form.setFieldsValue({
        // assignLevel: undefined,
        // assignLevelMeaning: undefined,
        // assignLevelValue: undefined,
        // assignLevelValueMeaning: undefined,
        tenantName: record.assignLevelValueMeaning
      });
    }
  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(selectedRowKeys, selectedRows) {
      this.setState({
        selectedRows: selectedRows,
        selectedRowKeys: selectedRows.map(function (record) {
          return record[rowKey];
        })
      });
    }
  }, {
    key: "getCheckboxProps",
    value: function getCheckboxProps(record) {
      if (record.admin) {
        return {
          disabled: true
        };
      } else {
        return {};
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          _this$props2$dataSour = _this$props2.dataSource,
          dataSource = _this$props2$dataSour === void 0 ? [] : _this$props2$dataSour,
          pagination = _this$props2.pagination,
          onChange = _this$props2.onChange,
          path = _this$props2.path,
          _this$props2$queryLoa = _this$props2.queryLoading,
          queryLoading = _this$props2$queryLoa === void 0 ? false : _this$props2$queryLoa,
          _this$props2$saveLoad = _this$props2.saveLoading,
          saveLoading = _this$props2$saveLoad === void 0 ? false : _this$props2$saveLoad,
          _this$props2$deleteLo = _this$props2.deleteLoading,
          deleteLoading = _this$props2$deleteLo === void 0 ? false : _this$props2$deleteLo;
      var _this$state$selectedR2 = this.state.selectedRowKeys,
          selectedRowKeys = _this$state$selectedR2 === void 0 ? [] : _this$state$selectedR2;
      var columns = this.getColumns();
      var rowSelection = {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleRowSelectionChange,
        getCheckboxProps: this.getCheckboxProps
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
        className: "table-operator"
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.addAssign"),
          type: 'button',
          meaning: '角色管理-新增'
        }],
        type: "primary",
        onClick: this.handleAddBtnClick
      }, intl.get('cpaas.common.button.add').d('新增')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.deleteAssign"),
          type: 'button',
          meaning: '角色管理-删除'
        }],
        onClick: this.handleDeleteBtnClick,
        disabled: selectedRowKeys.length === 0 || saveLoading || queryLoading,
        loading: deleteLoading
      }, intl.get('cpaas.common.button.delete').d('删除'))), /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        rowKey: rowKey,
        dataSource: dataSource,
        pagination: pagination,
        loading: queryLoading,
        rowSelection: rowSelection,
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        onChange: onChange
      }));
    }
  }]);

  return EditDataTable;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleAddBtnClick", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleAddBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleDeleteBtnClick", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleDeleteBtnClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleUserChange", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "handleUserChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleRowSelectionChange", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "handleRowSelectionChange"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "getCheckboxProps", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "getCheckboxProps"), _class.prototype)), _class));
export { EditDataTable as default };